package com.ptszyxx.popose.module.base.user.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.ActionEnum;
import com.ptszyxx.popose.common.oos.YOosUtil;
import com.ptszyxx.popose.common.utils.YActionEventUtil;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.utils.YChannelUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.utils.YMD5Utils;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserInfoImproveVM extends BaseViewModel<CommonRepository> {
    public ObservableField<String> nick = new ObservableField<>();
    public ObservableField<String> birthday = new ObservableField<>();
    public ObservableField<String> inviteCode = new ObservableField<>();
    public ObservableField<String> password = new ObservableField<>();
    public ObservableField<String> avatarPath = new ObservableField<>();
    public ObservableField<String> sex = new ObservableField<>("0");


    public UserInfoImproveVM(@NonNull Application application, CommonRepository repository) {
        super(application, repository);
    }

    @Override
    public void requestUploadAction() {
        YActionEventUtil.getInstance().upload(this, model, ActionEnum.user_improve);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onPicEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onBirthDayEvent = new SingleLiveEvent<>();
    }


    public BindingCommand onSubmitCommand = new BindingCommand(() -> requestUploadPic());
    public BindingCommand onPicCommand = new BindingCommand(() -> uc.onPicEvent.call());
    public BindingCommand onBirthDayCommand = new BindingCommand(() -> uc.onBirthDayEvent.call());
    public BindingCommand onBoyCommand = new BindingCommand(() -> sex.set("1"));
    public BindingCommand onGirlCommand = new BindingCommand(() -> sex.set("2"));

    public void requestOosToken(){
        YOosUtil.getInstance().getOosToken(model, this);
    }

    private void requestUploadPic() {
        if (!check())return;
        String userId = YSPUtils.getInstance().getUserId();
        YOosUtil.getInstance().upload(avatarPath.get(), userId, true,  new YOosUtil.OnOosListener() {
            @Override
            public void onSuccess(String url) {
                requestUserInfo(url);
            }

            @Override
            public void onSuccess(List<String> list) {

            }

            @Override
            public void onError() {
            }
        });
    }

    private void requestUserInfo(String picUrl) {
        if (!check())return;
        Map<String, String> map = new HashMap<>();
        map.put("nicheng", nick.get());
        map.put("sex", sex.get());
        if (YStringUtil.isNotEmpty(birthday.get())){
            map.put("shengri", birthday.get());
        }
        if (YStringUtil.isNotEmpty(inviteCode.get())){
            map.put("yaoqingma", inviteCode.get());
        }
        if (YStringUtil.isNotEmpty(password.get())){
            map.put("password", YMD5Utils.get(password.get()));
        }
        if (YStringUtil.isNotEmpty(picUrl)){
            map.put("pic", picUrl);
        }
        HttpUtils.getInstance()
                .data(model.userInfoImprove(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YActivityUtil.getInstance().jumpMainActivity(UserInfoImproveVM.this, null);
                            }
                        });
    }


    private boolean check(){
        if (!YChannelUtil.getInstance().huawei()){
            if (YStringUtil.isEmpty(avatarPath.get())){
                YToastUtil.showShort(R.string.avatar_hint);
                return false;
            }
        }
        if (YStringUtil.isEmpty(nick.get())){
            YToastUtil.showShort(R.string.et_nick_hint);
            return false;
        }
        if (YStringUtil.eq(0, sex.get())){
            YToastUtil.showShort(R.string.et_sex_hint);
            return false;
        }
        return true;
    }
}
